﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using System.Xml;
using KeyCore.Domain.Model;
using KeyCore.Framework.Contexts;
using KeyCore.Framework.DbInit;
using KeyCore.Framework.Repositories;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ""));
            using (var dc = new KeyCoreDbContext())
            {
               // if (!dc.Database.Exists())
                   dc.Database.Initialize(true);
            }

            using (var ctx = new LoansClientContext())
            {
       //         ctx.LoansClients.Load();
                //ctx.AdresTyp.Load(); 
                //var query = from s in ctx.AdresTyp select s;
                //Debug.Print(query.FirstOrDefault().AdresTypNazwa);
      
            }

            using (var repo = new LoansClientRepository())
            {
                var lc = new LoansClient();
             //   var r = repo.Context.Set<LoansClientContext>().Create().LoansClients;
                lc.ImiePierwsze = "Imie";
                lc.Nazwisko = "nazwisko";
                lc.NazwiskoRodowe = "nazrodowe";
                lc.DaneOsobowe = new DaneOsobowe();
                //lc = repo.GetC().FirstOrDefault(i => i.ImiePierwsze == "b");
            //    
                repo.CreateLoansClient(lc);
            }
            Console.ReadKey();

        }
    }


            //using (var ur = new UserRepository())
            //{
            //   // ur.CreateUser("User", "password", "email@email.pl", true);
            //   Console.WriteLine(ur.GetUser("User").UserName);
            //}

            //var mcs = new MembershipCreateStatus();
            //Membership.CreateUser("TestW", "pass", "emailw@w.pl", null, null, true, null, out mcs);
            //Console.WriteLine(mcs);
            //Console.ReadKey();
//    public class DontDropExistingDbCreateTablesIfModelChanged<T> : IDatabaseInitializer<T> where T : DbContext
//    {
//        private EdmMetadata edmMetaData;

//        public bool TryInitializeDatabase(T context)
//        {
//            ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
//            string modelHash = GetModelHash(objectContext);

//            if (CompatibleWithModel(modelHash, context, objectContext))
//                return false;

//            DeleteExistingTables(objectContext);
//            CreateTables(objectContext);
//            SaveModelHashToDatabase(context, modelHash, objectContext);

//            return true;
//        }

//        public void InitializeDatabase(T context)
//        {
//            TryInitializeDatabase(context);
//        }

//        private void SaveModelHashToDatabase(T context, string modelHash, ObjectContext objectContext)
//        {
//            if (edmMetaData != null) 
//                object(context as DbContext).Detach(edmMetaData);

//            edmMetaData = new EdmMetadata();
//            (context as DbContext).Set<EdmMetadata>().Add(edmMetaData);

//            edmMetaData.ModelHash = modelHash;
//            (context as DbContext).SaveChanges();
//        }

//        private void CreateTables(ObjectContext objectContext)
//        {
//            string dataBaseCreateScript = object(context as DbContext).CreateDatabaseScript();
//            object(context as DbContext).ExecuteStoreCommand(dataBaseCreateScript);
//        }

//        private void DeleteExistingTables(ObjectContext objectContext)
//        {
//            object(context as DbContext).ExecuteStoreCommand(DeleteAllTablesScript);
//        }

//        private string GetModelHash(ObjectContext context)
//        {
//            var csdlXmlString = GetCsdlXmlString(context).ToString();
//            return ComputeSha256Hash(csdlXmlString);
//        }

//        public bool CompatibleWithModel(DbContext context)
//        {
//            ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
//            return CompatibleWithModel(GetModelHash(objectContext), context, objectContext);
//        }

//        private bool CompatibleWithModel(string modelHash, DbContext context, ObjectContext objectContext)
//        {
//            var isEdmMetaDataInStore = object(context as DbContext).ExecuteStoreQuery<int>(LookupEdmMetaDataTable).FirstOrDefault();
//            if (isEdmMetaDataInStore == 1)
//            {
//                edmMetaData = (context as DbContext).Set<EdmMetadata>().FirstOrDefault();
//                if (edmMetaData != null)
//                {
//                    return modelHash == edmMetaData.ModelHash;
//                }
//            }
//            return false;
//        }

//        private string GetCsdlXmlString(ObjectContext context)
//        {
//            if (context != null)
//            {
//                var entityContainerList = (context as DbContext).MetadataWorkspace.GetItems<EntityContainer>(DataSpace.SSpace);
//                if (entityContainerList != null)
//                {
//                    EntityContainer entityContainer = entityContainerList.FirstOrDefault();
//                    var generator = new EntityModelSchemaGenerator(entityContainer);
//                    var stringBuilder = new StringBuilder();
//                    var xmlWRiter = XmlWriter.Create(stringBuilder);
//                    generator.GenerateMetadata();
//                    generator.WriteModelSchema(xmlWRiter);
//                    xmlWRiter.Flush();
//                    return stringBuilder.ToString();
//                }
//            }
//            return string.Empty;
//        }

//        private static string ComputeSha256Hash(string input)
//        {
//            byte[] buffer = new SHA256Managed().ComputeHash(Encoding.ASCII.GetBytes(input));
//            var builder = new StringBuilder(buffer.Length * 2);
//            foreach (byte num in buffer)
//            {
//                builder.Append(num.ToString("X2", CultureInfo.InvariantCulture));
//            }
//            return builder.ToString();
//        }

//        private const string DeleteAllTablesScript =
//            @"declare @cmd varchar(4000)
//
//              DECLARE cmds0 CURSOR FOR 
//              SELECT 'ALTER TABLE ' + TABLE_NAME + ' DROP CONSTRAINT ' + CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
//
//              DECLARE cmds1 CURSOR FOR 
//              SELECT 'ALTER TABLE ' + TABLE_NAME + ' DROP CONSTRAINT ' + CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
//
//              DECLARE cmds2 CURSOR FOR 
//              SELECT 'TRUNCATE TABLE ' + TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
//
//              DECLARE cmds3 CURSOR FOR 
//              SELECT 'DROP TABLE [' + TABLE_NAME + ']' FROM INFORMATION_SCHEMA.TABLES
//
//              open cmds0
//              while 1=1
//              begin
//                  fetch cmds0 into @cmd
//                  if @@fetch_status != 0 break
//                  print @cmd
//                  exec(@cmd)
//              end
//              close cmds0
//              deallocate cmds0
//
//              open cmds1
//              while 1=1
//              begin
//                  fetch cmds1 into @cmd
//                  if @@fetch_status != 0 break
//                  print @cmd
//                  exec(@cmd)
//              end
//              close cmds1
//              deallocate cmds1
//
//              open cmds2
//              while 1=1
//              begin
//                  fetch cmds2 into @cmd
//                  if @@fetch_status != 0 break
//                  print @cmd
//                  exec(@cmd)
//              end
//              close cmds2
//              deallocate cmds2
//
//              open cmds3
//              while 1=1
//              begin
//                  fetch cmds3 into @cmd
//                  if @@fetch_status != 0 break
//                  print @cmd
//                  exec(@cmd)
//              end
//              close cmds3
//              deallocate cmds3";

//        private const string LookupEdmMetaDataTable =
//            @"Select COUNT(*) 
//              FROM INFORMATION_SCHEMA.TABLES T 
//              Where T.TABLE_NAME = 'EdmMetaData'";
//    }

}
